package mpi;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.838AD77C-8D84-8EE3-D06C-54D2E3461CC4]
// </editor-fold> 
public class ServerListCmd extends ListCmd implements IListCmd {

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.96F1AEE4-BCA3-E883-C760-14B581CAE680]
    // </editor-fold> 
    public ServerListCmd(MPIController controller) {
        this.controller = controller;
        this.Cmd = new ArrayList<MPICmd>();
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.717EC373-353D-4EBD-4E35-248000FDA335]
    // </editor-fold> 
    public void addCmd(MPICmd cmd) {
        Cmd.add(cmd);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.2EF5CF36-2257-922F-FE69-4432AE0E0BCC]
    // </editor-fold> 
    public boolean load(String fileName) {

        try {

            FileInputStream fStream = new FileInputStream(fileName);
            ObjectInputStream oStream = new ObjectInputStream(fStream);

            this.Cmd = (ArrayList<MPICmd>)oStream.readObject();
            
            for (MPICmd com : this.Cmd) {
                if (!com.controller.getPlatform().equals(this.controller.getPlatform())){
                    System.out.println("Command "+com.getName()+" for platform "+com.controller.getPlatform());
                }

                    com.controller = this.controller;
            }
            oStream.close();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServerListCmd.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.0D9913CE-7CF3-96FA-B438-3B68ED09741A]
    // </editor-fold> 
    public boolean save(String fileName) {
      
        try {
            FileOutputStream fStream = new FileOutputStream(fileName);
            ObjectOutputStream sStream = new ObjectOutputStream(fStream);
            sStream.writeObject(this.Cmd);
            sStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("File save Error!!!");
            return false;
        }

        return true;

    }

    public boolean idTrue(int idCmd) {
        if (idCmd < this.Cmd.size() && idCmd > -1) {
            return true;
        }
        return false;
    }
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.5E8969FB-3051-FE90-1DB7-4D74F6DEB895]
    // </editor-fold> 

    public MPICmd getById(int idCmd) {

        if (this.idTrue(idCmd)) {
            return this.Cmd.get(idCmd);
        }
        return null;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.0E4F49CA-7935-6C15-9176-80DE88FE1FDB]
    // </editor-fold> 
    public MPICmd getByName(String nameCmd) {

        for (int i = 0; i < this.Cmd.size() - 1; i++) {

            MPICmd cmd = (MPICmd) this.Cmd.get(i);
            if (cmd.getName().equals(nameCmd)) {
                return cmd;
            }
        }
        return null;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.B5835AC7-B1F2-BFE3-4D5E-F2F144D6DE09]
    // </editor-fold> 
    public boolean changeCmd(int idCmd, MPICmd newCmd) {

        if (this.idTrue(idCmd)) {
            this.Cmd.set(idCmd, newCmd);
        }
        return true;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.B0914B6E-C191-C22C-985A-4F8E927B8BEA]
    // </editor-fold> 
    public boolean deleteCmd(int idCmd) {

        if (this.idTrue(idCmd)) {
            this.Cmd.remove(idCmd);
        }
        return true;
    }
}

